Skip to content

TL/UCP: Update topo aware ring algorithm #1288

Open
Juee14Desai wants to merge 2 commits into
openucx:masterfrom
Juee14Desai:ucc-ring-algo
Open

TL/UCP: Update topo aware ring algorithm #1288
Juee14Desai wants to merge 2 commits into
openucx:masterfrom
Juee14Desai:ucc-ring-algo

Conversation

@Juee14Desai

Copy link
Copy Markdown
Collaborator

What

Add topology aware multi ring algorithms for allgather, reduce_scatter, and allreduce in TL/UCP. The ring algorithms use team->cuda_ring to route data along NVLink optimal paths with up to 8 parallel rings, instead of the default single ring.

Why ?

The default ring algorithms use a flat rank ordering that does not account for the underlying GPU interconnect topology. On multi GPU systems with NVLink, this results in suboptimal data routing transfers may traverse slower paths instead of direct NVLink links.

How ?

Allgather:

  • Rewritten to derive ring rank, peer, and block indices from the cuda_ring topology pattern instead of flat team rank.
  • Each of the up to 8 rings transfers its own sub block slice concurrently.
  • Algorithm auto selected for CUDA memory >4KB when cuda_ring is available via dynamic score string in allgather.c.
  • Service allgather decoupled into dedicated service_allgather_ring_start/progress functions in tl_ucp_service_coll.c so internal collectives continue using the flat rank ring.
Count Size (bytes) UCC master Time avg (us) UCC master BW avg (GB/s) UCC this PR Time avg (us) UCC this PR BW avg (GB/s)
1048576 4194304 1318.36 47.72 1107.44 56.81
2097152 8388608 2527.03 49.79 1355.50 92.83
4194304 16777216 4946.76 50.87 1738.13 144.79
8388608 33554432 9760.98 51.56 2051.20 245.38
16777216 67108864 19394.07 51.90 3189.02 315.66
33554432 134217728 38630.25 52.12 5770.31 348.90
67108864 268435456 77089.72 52.23 10886.80 369.85

Reduce_scatter:

  • Rewritten to use cuda_ring for multi ring topology aware transfers.
  • Each ring handles its own sub block slice with per ring GPU reductions via the executor before forwarding to the next peer.
  • Scratch buffer management simplified to a single ucc_mc_alloc/free per task lifetime.
Count Size (bytes) UCC master Time avg (us) UCC master BW avg (GB/s) UCC this PR Time avg (us) UCC this PR BW avg (GB/s)
16777216 67108864 4500.15 13.98 9472.58 6.64
33554432 134217728 4724.07 26.64 6875.58 18.30
67108864 268435456 6985.83 36.02 7527.26 33.43
134217728 536870912 11992.22 41.97 8311.25 60.56
268435456 1073741824 22032.55 45.69 10223.47 98.46
536870912 2147483648 42097.26 47.82 14125.28 142.53
1073741824 4294967296 82387.74 48.87 23308.91 172.75

Allreduce:

  • Monolithic implementation that fuses reduce_scatter and allgather into a single task/progress function, avoiding schedule overhead.
  • Phase 0 receives into scratch, reduces with the local dst block via GPU executor, then forwards the accumulated result. Phase 1 performs an in-place ring allgather.
  • Tagged send/recv counters are reset at the phase transition.
  • Auto selected for CUDA memory >4KB via dynamic score string in allreduce.c.
Count Size (bytes) UCC master Time avg (us) UCC master BW avg (GB/s) UCC this PR Time avg (us) UCC this PR BW avg (GB/s)
1048576 4194304 N/A N/A 999.99 7.86
2097152 8388608 N/A N/A 1095.41 14.36
4194304 16777216 N/A N/A 1290.22 24.38
8388608 33554432 N/A N/A 4255.08 14.79
16777216 67108864 N/A N/A 921.83 136.50
33554432 134217728 N/A N/A 1796.86 140.05
67108864 268435456 N/A N/A 3513.99 143.23

@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

@wfaderhold21

Copy link
Copy Markdown
Collaborator

@Juee14Desai @janjust I assume this will replace PR #1258 ?

@janjust

janjust commented Mar 24, 2026

Copy link
Copy Markdown
Collaborator

We talked about this, it shouldn't need to. If they are both good to go then let's have both ring and topo-aware ring and we can phase one out as needed.

@Juee14Desai Juee14Desai force-pushed the ucc-ring-algo branch 3 times, most recently from b80e124 to 9472c37 Compare March 31, 2026 05:48
@Juee14Desai Juee14Desai force-pushed the ucc-ring-algo branch 2 times, most recently from bd96bad to e827813 Compare April 1, 2026 20:51
@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

@janjust janjust requested a review from MamziB April 2, 2026 15:43
@Sergei-Lebedev Sergei-Lebedev added the ai-review start ai-review label Apr 2, 2026
@greptile-apps

greptile-apps Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds topology-aware multi-ring algorithms for allgather, reduce_scatter, and allreduce in TL/UCP, routing transfers along NVLink-optimal paths (up to 8 parallel rings via team->cuda_ring) and decouples the service allgather from the topo ring.

  • Allgather and reduce_scatter both gain dynamic score-string functions that auto-select the topo ring for CUDA memory >4KB when cuda_ring is present; the service allgather is correctly preserved as a separate flat-ring path.
  • Allreduce ring is implemented as a schedule chaining the new RS and AG sub-tasks, but no dynamic score-string function was added to tl_ucp_coll.c \u2014 the algorithm is registered but never automatically selected, so the benchmark gains shown in the description will not materialize without explicit user configuration.
  • Several correctness issues flagged in prior review rounds (source-buffer corruption for non-in-place RS, missing count-divisibility check in allgather, algorithm regression for odd-size CPU teams) remain unresolved in this revision.

Confidence Score: 3/5

The allreduce ring is never auto-selected and multiple correctness issues from prior reviews remain open.

The allreduce ring algorithm is fully implemented but dead for automatic use: tl_ucp_coll.c has no str_get_fn for allreduce, so the ring is silently skipped. The non-in-place reduce_scatter source-buffer corruption and the allgather count-truncation issue identified in previous rounds are still present.

src/components/tl/ucp/tl_ucp_coll.c (missing allreduce score-string integration), src/components/tl/ucp/reduce_scatter/reduce_scatter_ring.c (non-in-place source corruption), src/components/tl/ucp/allgather/allgather_ring.c (count divisibility)

Important Files Changed

Filename Overview
src/components/tl/ucp/tl_ucp_coll.c Wires up the new reduce_scatter dynamic score string correctly, but the allreduce ring algorithm has no corresponding str_get_fn — the ring allreduce is never auto-selected for CUDA workloads.
src/components/tl/ucp/allgather/allgather_ring.c Rewrites allgather ring to use the cuda_ring topology pattern with up to 8 parallel rings; start counter sentinel (1) and progress loop logic are internally consistent, but count-divisibility is not checked (flagged in previous review).
src/components/tl/ucp/reduce_scatter/reduce_scatter_ring.c Complete rewrite to topology-aware multi-ring; non-in-place path still corrupts source buffer (flagged in previous review); ping-pong scratch for persistent is correct; missing send==recv symmetry assertion.
src/components/tl/ucp/allreduce/allreduce_ring.c New schedule-based allreduce ring that chains RS+AG sub-tasks; functionally correct for in-place CUDA workloads, but never auto-selected because no dynamic score string was added to tl_ucp_coll.c; also lacks early cuda_ring guard before schedule allocation.
src/components/tl/ucp/tl_ucp_service_coll.c Extracts a dedicated service_allgather_ring_start/progress so internal collectives continue using the flat ring; function pointers are correctly initialized before use.

Reviews (6): Last reviewed commit: "TL/UCP: topo aware ring algo for reduce_..." | Re-trigger Greptile

Comment thread src/components/tl/ucp/allreduce/allreduce_ring.c Outdated
Comment on lines +157 to +160

send_idx = ucc_ring_pattern_get_send_block(ring, ring_id,
rrank, step);
ring_offset = ucc_buffer_block_offset(block_cnt, nrings, ring_id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing send_posted == recv_posted assertion

The allgather ring progress function asserts task->tagged.send_posted == task->tagged.recv_posted at the top of its loop (since sends and recvs are always posted in pairs). The allreduce ring's RS-phase loop has the send_posted > 0 / recv_posted > 0 guards but omits the equality check, making it harder to catch a bookkeeping bug early. Consider adding the same assertion for consistency and diagnosability.

Comment thread src/components/tl/ucp/reduce_scatter/reduce_scatter.c
Comment thread src/components/tl/ucp/allgather/allgather.c
Comment thread src/components/tl/ucp/allgather/allgather_ring.c Outdated
Comment thread src/components/tl/ucp/allgather/allgather_ring.c Outdated
Comment thread src/components/tl/ucp/allreduce/allreduce_ring.c Outdated
Comment thread src/components/tl/ucp/allreduce/allreduce_ring.c Outdated
Comment thread src/components/tl/ucp/allreduce/allreduce_ring.c Outdated
Comment thread src/components/tl/ucp/allreduce/allreduce_ring.c Outdated
Comment on lines +82 to +84
reduce_target = PTR_OFFSET(
sbuf,
(recv_block * block_cnt + ring_offset) * dt_size);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Non-in-place operation silently corrupts the source buffer

For non-in-place reduce_scatter, sbuf = args->src.info.buffer. On every non-final step the reduction writes back into that same buffer:

reduce_target = PTR_OFFSET(sbuf, (recv_block * block_cnt + ring_offset) * dt_size);

The subsequent send loop then reads from the now-mutated sbuf location to forward the intermediate partial sum. The old implementation accumulated partial results in the dedicated s_scratch send buffer and left sbuf read-only. Callers with separate source and destination buffers (i.e., any non-in-place reduce_scatter over CUDA memory when cuda_ring is present) will have their source data silently overwritten.

The simplest safe fix is to reject non-in-place in reduce_scatter_ring_init_common (analogous to the UCC_IS_PERSISTENT guard already there):

if (!UCC_IS_INPLACE(*args)) {
    return UCC_ERR_NOT_SUPPORTED;
}

Or alternatively, allocate a separate per-ring work buffer for intermediate reductions instead of reusing sbuf.

@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

2 similar comments
@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

@janjust

janjust commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

/build

@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

@Juee14Desai

Copy link
Copy Markdown
Collaborator Author

/build

Comment on lines 76 to +79
}
}

if (algo_num == UCC_TL_UCP_ALLGATHER_ALG_RING && !team->cuda_ring) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Algorithm regression for odd-size non-CUDA teams

When cuda_ring is NULL (any CPU team, or GPU team without NVLink topology), odd-size teams previously used the flat RING algorithm. After this guard, they fall through to KNOMIAL. This silently changes the default for every odd-size non-CUDA workload, including large CPU clusters.

The underlying reason is that allgather_ring_init_common now hard-fails with UCC_ERR_NOT_SUPPORTED when cuda_ring == NULL, so the score-string must avoid selecting it. However, the correct fallback for the "no-topology-info" case is still the flat ring (old behavior), not knomial. Consider keeping the flat-ring algorithm alive under a separate name/enum and using it as the fallback, or only switching to KNOMIAL when the caller is guaranteed to benefit.

Replace the default ring allgather with a topo aware
multi ring implementation that uses team->cuda_ring to route data
along NVLink optimal paths (up to 8 parallel rings).

Algorithm changes:
- Ring rank, peer, and block indices are now derived from the
  cuda_ring topology pattern instead of flat team rank ordering.
- Each ring transfers its own slice of each block, enabling
  concurrent data movement across multiple NVLink paths.
- Algorithm auto selected for CUDA memory >4KB when cuda_ring
  is available; falls back to knomial otherwise.

Also fixes CUDA primary context detection in ucc_sysinfo_cuda.c
and decouples the service allgather from the topo aware ring.

Signed-off-by: Juee Himalbhai Desai <jueehimalbha@nvidia.com>
Replace the default ring reduce_scatter with a topo aware
multi ring implementation that uses team->cuda_ring to route data
along NVLink optimal paths (up to 8 parallel rings).

Algorithm changes:
- Ring rank, peer, and block indices are now derived from the
  cuda_ring topology pattern instead of flat team rank ordering.
- Each ring handles its own sub block slice, with per ring GPU
  reductions via the executor before forwarding to the next peer.
- Scratch buffer management simplified to a single mc_alloc/free
  per task lifetime (removed fragmentation logic).

Signed-off-by: Juee Himalbhai Desai <jueehimalbha@nvidia.com>

@wfaderhold21 wfaderhold21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correct, this PR is removing CPU-based allgather/reduce_scatter ring algorithms. We will need a future PR to bring those back before 1.9.0 release.

ucc_rank_t tsize = ucc_ring_pattern_size(ring, 0);
ucc_rank_t block = UCC_TL_TEAM_RANK(team);
size_t data_size = (count / tsize) * ucc_dt_size(dt);
ucc_status_t status;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2021-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026

if (!UCC_IS_INPLACE(*args)) {
status = ucc_mc_memcpy(PTR_OFFSET(rbuf, data_size * block),
sbuf, data_size, rmem, smem);
sbuf, data_size, rmem, smem);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

@@ -1,424 +1,308 @@
/**
* Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026

step - 1);
data_displ = (send_block * block_cnt + ring_offset) * dt_size;
send_src = PTR_OFFSET(sbuf, data_displ);
next_recv_dst = PTR_OFFSET(scratch, ring_offset * dt_size);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

void *scratch = task->reduce_scatter_ring.scratch;
ucc_rank_t ring_id, rrank, adj_rrank, send_block, sendto, recvfrom;
size_t ring_offset, ring_count, data_displ, data_size;
ucc_status_t status;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

ucc_tl_ucp_team_t *tl_team;
ucc_status_t status;
ucc_tl_ucp_team_t *team = TASK_TEAM(task);
ucc_coll_args_t *args = &TASK_ARGS(task);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

size_t count = TASK_ARGS(task).dst.info.count;
size_t data_size = (count / tsize) * ucc_dt_size(TASK_ARGS(task).dst.info.datatype);
ucc_rank_t sendto, recvfrom, sblock, rblock;
int step;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants